package io.renren.modules.erp.controller;

import io.renren.common.annotation.SysLog;
import io.renren.common.constants.ExceptionConstants;
import io.renren.common.exception.BusinessRunTimeException;
import io.renren.common.utils.Constants;
import io.renren.common.utils.QueryUtils;
import io.renren.common.utils.R;
import io.renren.common.utils.StringUtil;
import io.renren.modules.erp.entity.MaterialProperty;
import io.renren.modules.erp.service.MaterialPropertyService;
import io.renren.modules.erp.service.impl.MaterialPropertyServiceImpl;
import io.renren.modules.sys.controller.AbstractController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping(value = "/materialProperty")
public class MaterialPropertyController extends AbstractController {
    private Logger logger = LoggerFactory.getLogger(MaterialPropertyController.class);
    @Autowired
    private MaterialPropertyServiceImpl materialPropertyService;

    @GetMapping(value = "/list")
    public  R getMaterialPropertyList(@RequestParam Map<String, String> params)throws Exception {
        String search = params.get(Constants.SEARCH);
        String name = StringUtil.getInfo(search, "name");
        String order = QueryUtils.order(params);
        int pageSize = Integer.valueOf(params.get( "pageSize"));
        int currPage = Integer.valueOf(params.get( "currentPage"));
        return R.ok().put("data",materialPropertyService.select(name, currPage, pageSize));
    }

    @SysLog("保存")
    @PostMapping("/save")
    public R save(@RequestBody MaterialProperty materialProperty){
        materialPropertyService.save(materialProperty);
        return R.ok();
    }
    /**
     * create by: qiankunpingtai
     * website：https://qiankunpingtai.cn
     * description:
     *  批量删除商品扩展信息
     * create time: 2019/3/29 11:15
     * @Param: ids
     * @return java.lang.Object
     */
    @RequestMapping(value = "/batchDeleteMaterialPropertyByIds")
    public R batchDeleteMaterialPropertyByIds(@RequestParam("ids") String ids, HttpServletRequest request) throws Exception {
        int i= materialPropertyService.batchDeleteMaterialPropertyByIds(ids,getUserId());
        if(i<1){
            logger.error("异常码[{}],异常提示[{}],参数,ids[{}]",
                    ExceptionConstants.MATERIAL_PROPERTY_DELETE_FAILED_CODE,ExceptionConstants.MATERIAL_PROPERTY_DELETE_FAILED_MSG,ids);
            throw new BusinessRunTimeException(ExceptionConstants.MATERIAL_PROPERTY_DELETE_FAILED_CODE,
                    ExceptionConstants.MATERIAL_PROPERTY_DELETE_FAILED_MSG);
        }
        return R.ok();
    }

    @PostMapping(value = "/add")
    public R insert(@RequestBody String beanJson, HttpServletRequest request)throws Exception {
        if(1==materialPropertyService.insertMaterialProperty(beanJson, request)){
            return R.ok();
        }else{
            return R.error();
        }
    }

    @PostMapping(value = "/update")
    public R update(@RequestBody String beanJson,@RequestParam("id")  Long id, HttpServletRequest request)throws Exception {
        if(1==materialPropertyService.updateMaterialProperty(beanJson, id, request)){
            return R.ok();
        }else{
            return R.error();
        }
    }

    @GetMapping(value = "/delete")
    public R delete(@RequestParam("id")Long id, HttpServletRequest request)throws Exception {
        if(1==materialPropertyService.deleteMaterialProperty(id, request)){
            return R.ok();
        }else{
            return R.error();
        }
    }

    @GetMapping(value = "/batchDelete")
    public R batchDelete(@RequestParam("ids")String ids, HttpServletRequest request)throws Exception {
        if(1==materialPropertyService.batchDeleteMaterialProperty(ids, request)){
            return R.ok();
        }else{
            return R.error();
        }
    }

    @GetMapping(value = "/checkIsNameExist")
    public R checkIsNameExist(@RequestParam("id") Long id, @RequestParam("name")String name)throws Exception {
        if(1<materialPropertyService.checkIsNameExist(id, name)){
            return R.ok();
        }else{
            return R.error();
        }
    }
}
